package com.zznj.xzjc.activity.index;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.zznj.xzjc.R;
import com.zznj.xzjc.application.APP;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.fragment.index.PushFragment;
import com.zznj.xzjc.fragment.ztgk.AjblFragment;
import com.zznj.xzjc.fragment.ztgk.JdzxFragment;
import com.zznj.xzjc.fragment.ztgk.JrdwFragment;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.utils.statusbar.StatusBarUtil;
import com.zznj.xzjc.widget.FragmentAda;
import com.zznj.xzjc.widget.MyViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述：线索发现
 */
public class ActivityZtgkMain extends AppCompatActivity {
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.main_viewpager)
    MyViewPager mViewPager;

    @BindView(R.id.titleLinear)
    LinearLayout titleLinear;
    @BindView(R.id.backBtn)
    View backBtn;


    Unbinder unbinder;


    private String[] mTitle = new String[]{"接入单位", "案件办理", "监督执行"};
    private ArrayList<Fragment> fragList = new ArrayList<Fragment>();
    private int selectIndex = 0;

    private LoginBean user;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gyss_activity_ztgk_main_layout);
        APP.getInstance().add(this);
        ButterKnife.bind(this);
        selectIndex = getIntent().getExtras().getInt("selectIndex");
        StatusBarUtil.setStatusBar(this, true, R.color.transparent, false, false);
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(this, "Member"), LoginBean.class);
        initFragments();
        titleLinear.setPadding(0, com.zznj.xzjc.view.StatusBarUtil.getStatusBarHeight(this), 0, 0);
    }

    private void initFragments() {
        fragList.clear();
        fragList.add(JrdwFragment.getInstance());
        fragList.add(AjblFragment.getInstance());
        fragList.add(JdzxFragment.getInstance());

        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mViewPager.setAdapter(new FragmentAda(getSupportFragmentManager(), fragList, null));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.setViewPager(mViewPager, mTitle);

        mViewPager.setCurrentItem(selectIndex);
        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    @OnClick({R.id.backBtn})
    public void onClick() {
        finish();
    }
}